package org.ktm.exception;

import java.util.Locale;
import org.ktm.utils.KTMResources;
import org.ktm.utils.Localizer;

public class KTMError extends Error {

    private static final long serialVersionUID = 1L;

    /**
     * Construct an error with no replacement values.
     * 
     * @param key
     *            Message key for this error message
     */
    public KTMError(String key) {
        this(key, null, null, null, null);
    }

    /**
     * Construct an error with no replacement values.
     * 
     * @param key
     *            Message key for this error message
     * @param exception
     *            Exception attached to this error
     */
    public KTMError(String key, KTMException exception) {

        this(key, null, null, null, null, exception);

    }

    /**
     * Construct an error with the specified replacement values.
     * 
     * @param key
     *            Message key for this error message
     * @param value0
     *            First replacement value
     */
    public KTMError(String key, Object value0) {

        this(key, value0, null, null, null);

    }

    /**
     * Construct an error with the specified replacement values.
     * 
     * @param key
     *            Message key for this error message
     * @param value0
     *            First replacement value
     * @param exception
     *            Exception attached to this error
     */
    public KTMError(String key, Object value0, KTMException exception) {

        this(key, value0, null, null, null, exception);

    }

    /**
     * Construct an error with the specified replacement values.
     * 
     * @param key
     *            Message key for this error message
     * @param value0
     *            First replacement value
     * @param value1
     *            Second replacement value
     */
    public KTMError(String key, Object value0, Object value1) {

        this(key, value0, value1, null, null);

    }

    /**
     * Construct an error with the specified replacement values.
     * 
     * @param key
     *            Message key for this error message
     * @param value0
     *            First replacement value
     * @param value1
     *            Second replacement value
     * @param exception
     *            Exception attached to this error
     */
    public KTMError(String key,
            Object value0,
            Object value1,
            KTMException exception) {

        this(key, value0, value1, null, null, exception);

    }

    /**
     * Construct an error with the specified replacement values.
     * 
     * @param key
     *            Message key for this error message
     * @param value0
     *            First replacement value
     * @param value1
     *            Second replacement value
     * @param value2
     *            Third replacement value
     */
    public KTMError(String key, Object value0, Object value1, Object value2) {

        this(key, value0, value1, value2, null);

    }

    /**
     * Construct an error with the specified replacement values.
     * 
     * @param key
     *            Message key for this error message
     * @param value0
     *            First replacement value
     * @param value1
     *            Second replacement value
     * @param value2
     *            Third replacement value
     * @param exception
     *            Exception attached to this error
     */
    public KTMError(String key,
            Object value0,
            Object value1,
            Object value2,
            KTMException exception) {

        this(key, value0, value1, value2, null, exception);

    }

    /**
     * Construct an error with the specified replacement values.
     * 
     * @param key
     *            Message key for this error message
     * @param value0
     *            First replacement value
     * @param value1
     *            Second replacement value
     * @param value2
     *            Third replacement value
     * @param value3
     *            Fourth replacement value
     */
    public KTMError(String key,
            Object value0,
            Object value1,
            Object value2,
            Object value3) {

        super();
        this.key = key;
        values[0] = value0;
        values[1] = value1;
        values[2] = value2;
        values[3] = value3;

    }

    /**
     * Construct an error with the specified replacement values.
     * 
     * @param key
     *            Message key for this error message
     * @param value0
     *            First replacement value
     * @param value1
     *            Second replacement value
     * @param value2
     *            Third replacement value
     * @param value3
     *            Fourth replacement value
     */
    public KTMError(String key,
            Object value0,
            Object value1,
            Object value2,
            Object value3,
            KTMException exception) {

        super();
        this.key = key;
        values[0] = value0;
        values[1] = value1;
        values[2] = value2;
        values[3] = value3;
        this.exception = exception;

    }

    // ----------------------------------------------------- Instance Variables

    /**
     * The message key for this error message.
     */
    private String       key       = null;

    /**
     * The replacement values for this error mesasge.
     */
    private Object       values[]  = {
                                   null, null, null, null
                                   };

    /**
     * The Exception related to this error message.
     */
    private KTMException exception = null;

    // --------------------------------------------------------- Public Methods

    /**
     * Get the message key for this error message.
     */
    public String getKey() {

        return (this.key);

    }

    /**
     * Get the replacement values for this error message.
     */
    public Object[] getValues() {

        return (this.values);

    }

    public KTMException getException() {

        return (this.exception);
    }

    public String getMessage() {
        String result = null;
        try {
            result = new KTMResources().getString(getKey(), this, Localizer.getCurrentLocale());
        } catch (Exception exception) {
            result = exception.toString() + ": " + getKey();
        }
        return result;
    }

    public String getMessage(Locale locale) {
        String result = null;
        try {
            result = new KTMResources().getString(getKey(), this, locale);
        } catch (Exception ex) {
            result = ex.toString() + ": " + getKey();
        }
        return result;
    }
}
